package org.example.serve;

import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import org.example.RpcApplication;
import org.example.pojo.RpcRequest;
import org.example.pojo.RpcResponse;
import org.example.registry.LocalRegistry;
import org.example.serializer.JdkSerializer;
import org.example.serializer.Serializer;
import org.example.serializer.SerializerFactory;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 处理请求和响应
 */
public class HttpServerHandle implements Handler<HttpServerRequest> {
    @Override
    public void handle(HttpServerRequest httpServerRequest) {
        //准备序列化器
//        Serializer jdkSerializer = new JdkSerializer();
        Serializer jdkSerializer = SerializerFactory.getInstance(RpcApplication.getRpcConfig().getSerializer());
        System.out.println("接受到请求："+httpServerRequest.method());
        httpServerRequest.bodyHandler(body ->{
            byte[] bytes = body.getBytes();
            RpcRequest request =null;
            try {
                //将客户端请求的数据进行反序列化并赋给rpcRequest
                request = jdkSerializer.deserialize(bytes, RpcRequest.class);
                System.out.println("方法名：" + request.getMethodName());
                System.out.println("参数类型：" + Arrays.toString(request.getParameterTypes()));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            //构造响应结果对象
            RpcResponse rpcResponse = new RpcResponse();
            //如果请求为空
            if (request==null){
                rpcResponse.setMessage("请求数据为空");
                doResponse(httpServerRequest,rpcResponse,jdkSerializer);
                return;
            }
            try {
            Class aClass = LocalRegistry.get(request.getServiceName());
                Method method = aClass.getMethod(request.getMethodName(),request.getParameterTypes());
                Object invoke = method.invoke(aClass.newInstance(), request.getParameters());
                rpcResponse.setData(invoke);
                rpcResponse.setDataType(invoke.getClass());
                rpcResponse.setMessage("请求成功");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            doResponse(httpServerRequest,rpcResponse,jdkSerializer);
        });
    }

    /**
     * 响应内容
     * @param httpServerRequest
     * @param rpcResponse
     * @param serializer
     */
    void doResponse(HttpServerRequest httpServerRequest,RpcResponse rpcResponse,Serializer serializer){
        HttpServerResponse response = httpServerRequest.response().putHeader("content-type", "application/json");
        try {
            byte[] serialize = serializer.serialize(rpcResponse);
            response.end(Buffer.buffer(serialize));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
